package com.hy.controller;

import com.hy.pojo.*;
import com.hy.service.impl.*;
import com.hy.vo.Formulas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

@RestController
public class getBinary {
    @Resource
    private SequenceServiceImpl sequenceService;
    @Resource
    private SequenceAdditionServiceImpl sequenceAdditionService;
    @Resource
    private SequenceDivisionServiceImpl sequenceDivisionService;
    @Resource
    private SequenceMultiplicationServiceImpl sequenceMultiplicationService;
    @Resource
    private SequenceSubtractionServiceImpl sequenceSubtractionService;



    /**
     * @作用 拿到四十条随机数据
     * @return
     */
    @GetMapping("/all")
    public Collection<Formulas> all() {
        int count = sequenceService.count();
        Random random = new Random();
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            int num = random.nextInt(count-1) + 1;
            Sequence sequence = sequenceService.getById(num);
            Formulas formula = new Formulas();
            //字符串转化
            if (sequence.getSymbol() == '/') {
                sequence.setSymbol('÷');
            } else if (sequence.getSymbol() == '*') {
                sequence.setSymbol('x');
            }
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        }

        return formulas;
    }

    /**
     * @作用 拿到四十条加法数据
     * @return
     */
    @GetMapping("/getAddition")
    public Collection<Formulas> getAddition() {
        int count = sequenceAdditionService.count();

        Random random = new Random();
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            int num = random.nextInt(count-1) + 1;
            SequenceAddition sequence = sequenceAdditionService.getById(num);
            Formulas formula = new Formulas();
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        }

        return formulas;
    }

    /**
     * @作用 拿到四十条除法数据
     * @return
     */
    @GetMapping("/getDivision")
    public Collection<Formulas> getDivision() {
        int count = sequenceDivisionService.count();

        Random random = new Random();
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            int num = random.nextInt(count-1) + 1;
            SequenceDivision sequence = sequenceDivisionService.getById(num);
            Formulas formula = new Formulas();
            //字符串转化
            sequence.setSymbol('÷');
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        }

        return formulas;
    }

    /**
     * @作用 拿到四十条乘法数据
     * @return
     */
    @GetMapping("/getMultiplication")
    public Collection<Formulas> getMultiplication() {
        int count = sequenceMultiplicationService.count();

        Random random = new Random();
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            int num = random.nextInt(count-1) + 1;
            SequenceMultiplication sequence = sequenceMultiplicationService.getById(num);
            Formulas formula = new Formulas();
            //字符串转化
            sequence.setSymbol('x');
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        }

        return formulas;
    }

    /**
     * @作用 拿到四十条减法数据
     * @return
     */
    @GetMapping("/getSubtraction")
    public Collection<Formulas> getSubtraction() {
        int count = sequenceSubtractionService.count();

        Random random = new Random();
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            int num = random.nextInt(count-1) + 1;
            SequenceSubtraction sequence = sequenceSubtractionService.getById(num);
            Formulas formula = new Formulas();
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        }

        return formulas;
    }
}
